from django.db import models

# Create your models here.
class Grade(models.Model):
    name = models.CharField(max_length=20)
    boyNum = models.IntegerField()
    girlNum = models.IntegerField()
    isDelete = models.BooleanField(default=False)
    def __str__(self):
        return self.name
    class Meta():
        db_table = "grades"
        ordering = ['id']

class StudentManager(models.Manager):  # 模型管理器
    def get_queryset(self):
        return super(StudentManager,self).get_queryset().filter(isDelete = False)

class Student(models.Model):
    objects = StudentManager()   # 自定义一个对象

    name = models.CharField(max_length=20)
    sex = models.BooleanField()
    age = models.IntegerField()
    content = models.CharField(max_length=40)
    grade = models.ForeignKey("grade")
    isDelete = models.BooleanField(default=False)
    def __str__(self):
        return self.name
    class Meta():
        db_table = "students"
        ordering = ["-id"]

    @classmethod   # 类方法, 类调用
    def create(cls, name, sex, age, content, grade):
        obj = cls(name=name,sex =sex, age=age, content= content, grade =grade)
        return  obj
    def duty(self):
        return "keep learning"


class IdCard(models.Model):
    sex = models.BooleanField()
    num = models.CharField(max_length=20)
    class Meta():
        db_table = "IdCard"
        ordering = ['id']

class Person(models.Model):
    name = models.CharField(max_length=20)
    age = models.IntegerField()
    # idCard = models.OneToOneField(IdCard, on_delete=models.CASCADE) # 默认
    idCard = models.OneToOneField(IdCard, on_delete=models.PROTECT) # 此时无法删除身份证
    # 关系存放在哪张卡中都可以
    #绑定卡与人的一对一关系, 默认情况下当删除身份证时,绑定的人也可以删除,
#     通过on_delete
    class Meta():
        db_table = "Person"
        ordering = ['id']


class Buyer(models.Model):
    name = models.CharField(max_length=20)
    lebel = models.IntegerField()
    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=20)
    price = models.IntegerField()
    # 关系形成
    buyers = models.ManyToManyField(Buyer)
    def __str__(self):
        return self.name


class Animal(models.Model):
    name = models.CharField(max_length=20)
    age = models.IntegerField()
    class Meta():
        # 让该类抽象
        abstract = True

class Cat(Animal):
    mao = models.CharField(max_length=20)
class Dog(Animal):
    gou = models.CharField(max_length=20)


# 富文本
from tinymce.models import HTMLField
class Article(models.Model):
    title = models.CharField(max_length=20)
    content = HTMLField()





